package org.example.bucket;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class FlinkDynamicBucketTable {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setString(RestOptions.BIND_PORT,"8081");
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(conf);
        env.setParallelism(1);
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        TableEnvironment tableEnv = StreamTableEnvironment.create(env);

        // 创建 Catalog
        String createCatalogSQL = "CREATE CATALOG paimon WITH (\n" +
                "    'type' = 'paimon',\n" +
                "    'warehouse' = 'file:///tmp/paimon'\n" +
                ");\n";
        System.out.println("Executing SQL:\n" + createCatalogSQL);
        tableEnv.executeSql(createCatalogSQL).print();
        
        // 创建数据生成表
        String createDataGenSQL = "CREATE TABLE behavior_gen (\n" +
                "    user_id STRING,\n" +
                "    item_id STRING,\n" +
                "    behavior BIGINT,\n" +
                "    dt AS (CASE\n" +
                "        WHEN RAND() < 0.33 THEN '2025-01-09'\n" +
                "        WHEN RAND() < 0.66 THEN '2024-10-01'\n" +
                "        ELSE '2023-01-01'\n" +
                "    END),\n" +
                "    eventtime TIMESTAMP_LTZ(3)\n" +
                ") WITH (\n" +
                "  'connector' = 'datagen'\n" +
                "  , 'rows-per-second' = '10'\n" +
                ");";
        System.out.println("Executing SQL:\n" + createDataGenSQL);
        tableEnv.executeSql(createDataGenSQL).print();

        // 删除已存在的表
        String dropTableSQL = "DROP TABLE IF EXISTS `paimon`.`default`.`user_behavior`";
        System.out.println("Executing SQL:\n" + dropTableSQL);
        tableEnv.executeSql(dropTableSQL).print();

        // 创建目标表
        String createTableSQL = "CREATE TABLE IF NOT EXISTS `paimon`.`default`.`user_behavior`\n" +
                "(\n" +
                "    user_id STRING,\n" +
                "    item_id STRING,\n" +
                "    behavior BIGINT,\n" +
                "    dt STRING,\n" +
                "    eventtime TIMESTAMP_LTZ(3),\n" +
                "    WATERMARK FOR eventtime AS eventtime - INTERVAL '5' SECOND,\n" +
                "    PRIMARY KEY (dt, user_id) NOT ENFORCED\n" +
                ")\n" +
                "PARTITIONED BY (dt)\n" +
                "WITH (\n" +
                "    'partition.expiration-time' = '99999 d',\n" +
                "    'bucket' = '-1',\n" +
                "    'dynamic-bucket.target-row-num' = '10000',\n" +
                "    'dynamic-bucket.initial-buckets' = '1',\n" +
                "    想·" +
                ")\n";
        System.out.println("Executing SQL:\n" + createTableSQL);
        tableEnv.executeSql(createTableSQL).print();

        // 插入数据
        String insertSQL = "INSERT INTO `paimon`.`default`.`user_behavior`\n" +
                "SELECT\n" +
                "    user_id\n" +
                "    , item_id\n" +
                "    , behavior\n" +
                "    , dt\n" +
                "    , eventtime\n" +
                "FROM behavior_gen";
        System.out.println("Executing SQL:\n" + insertSQL);
        tableEnv.executeSql(insertSQL).print();
    }
}
